package com.biShe.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.biShe.entity.Addresses;
import com.biShe.entity.Category;
import com.biShe.entity.WechatUser;
import com.biShe.mapper.AddressesMapper;
import com.biShe.mapper.WechatUserMapper;
import com.biShe.utils.Result;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 小刘
 * @since 2023-05-08
 */
@Configuration
@RestController
@RequestMapping("/wechat-user")
public class WechatUserController {

    @Autowired
    private WechatUserMapper wechatUserMapper;

    /**
     * 查找所有微信用户
     *
     * @return 返回所有微信用户信息列表
     */
    @ApiOperation(value="获取所有微信用户", notes="返回所有微信用户列表")
    @GetMapping("/list")
    public Result list(){
        List<WechatUser> wechatUserList = wechatUserMapper.selectList(null);
        return Result.succ(wechatUserList);
    }

    /**
     * 根据openID查找微信用户
     *
     * @param openId 微信小程序的openId
     * @return 返回指定用户信息
     */
    @ApiOperation(value="根据openId获取微信用户信息", notes="返回指定openId的微信用户信息")
    @ApiImplicitParam(name = "openId", value = "微信openId", required = true, dataType = "String", paramType = "path")
    @GetMapping("/{openId}")
    public Result getById(@PathVariable("openId") String openId) {
       QueryWrapper<WechatUser> wrapper = new QueryWrapper<>();
       wrapper.eq("openId",openId);
       List<WechatUser> wechatUserList = wechatUserMapper.selectList(wrapper);

        if (wechatUserList.isEmpty()) {
            return Result.fail("该用户不存在",wechatUserList);
        }
        return Result.succ(wechatUserList);
    }

    /**
     * 小程序端添加新微信用户
     *
     * @param  wechatUser
     * @return 返回添加结果
     */
    @ApiOperation(value="添加新微信用户", notes="添加新微信用户信息")
    @ApiImplicitParam(name = "wechatUser", value = "微信用户实体", required = true, dataType = "wechatUser")
    @PostMapping("/add")
    public Result add(WechatUser wechatUser) {
        int rows = wechatUserMapper.insert(wechatUser);
        QueryWrapper<WechatUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("openid",wechatUser.getOpenid());//这里的column要和数据库保存一致
        WechatUser wechatUser1 = wechatUserMapper.selectOne(queryWrapper);
        if (rows == 0) {
            return Result.fail("添加新用户失败");
        }
        return Result.succ(wechatUser1);
    }

    /**
     * 小程序端更新部分用户信息，没收到的信息就不变
     *
     * @param wechatUser 地址信息
     * @return 返回更新结果
     */
    @ApiOperation(value="修改用户信息", notes="修改指定用户的信息")
    @ApiImplicitParam(name = "wechatUser", value = "用户实体", required = true, dataType = "WechatUser")
    @PutMapping("/update")
    public Result update(WechatUser wechatUser) {
        LambdaUpdateWrapper<WechatUser> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(WechatUser::getOpenid,wechatUser.getOpenid());
        updateWrapper.set(WechatUser::getNickname, wechatUser.getNickname());
        updateWrapper.set(WechatUser::getMobilephone, wechatUser.getMobilephone());
        updateWrapper.set(WechatUser::getBirthday, wechatUser.getBirthday());
        updateWrapper.set(WechatUser::getRegistertime, wechatUser.getRegistertime());

        int rows =  wechatUserMapper.update(null,updateWrapper);
        if (rows == 0) {
            return Result.fail("更新微信用户信息失败");
        }
        return Result.succ(rows);
    }

    /**
     * 删除微信用户信息
     *
     * @param openId 微信用户openId 条件删除
     * @return 返回删除结果
     */
    @ApiOperation(value="删除微信用户信息", notes="删除指定openID的微信用户信息")
    @ApiImplicitParam(name = "openId", value = "微信小程序openId", required = true, dataType = "Int", paramType = "path")
    @DeleteMapping("/{openId}")
    public Result delete(@PathVariable("openId") String openId) {
        QueryWrapper<WechatUser> wrapper = new QueryWrapper<>();
        wrapper.eq("openId",openId);
        int rows = wechatUserMapper.delete(wrapper);
        if (rows == 0) {
            return Result.fail("删除用户信息失败");
        }
        return Result.succ("删除用户信息成功");
    }

    /**
     * PC查找所有微信用户信息
     *
     * @return 返回所有分类信息列表
     */
    @ApiOperation(value="获取所有微信用户信息", notes="返回所有微信用户列表")
    @GetMapping("/list/{page}/{size}")
    public Result list(@PathVariable("page") String page, @PathVariable("size") String size) {
        Page pageHelper = PageHelper.startPage(Integer.parseInt(page),Integer.parseInt(size));
        List<WechatUser> categoryList = wechatUserMapper.selectList(null);
        PageInfo pageInfo = new PageInfo(categoryList);
        return Result.succ(pageInfo);
    }

    /**
     * 根据id删除微信用户信息
     *
     * @param id 分类ID
     * @return 返回删除结果
     */
    @ApiOperation(value="删除分类信息", notes="删除指定ID的分类信息")
    @ApiImplicitParam(name = "id", value = "分类ID", required = true, dataType = "Integer", paramType = "path")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") Integer id) {
        QueryWrapper<WechatUser> wrapper = new QueryWrapper<>();
        wrapper.eq("uId",id);
        int rows = wechatUserMapper.delete(wrapper);
        if (rows == 0) {
            return Result.fail("删除分类信息失败");
        }
        return Result.succ("删除分类信息成功");
    }


    /**
     * 根据用户名模糊查询用户信息
     *
     * @param name 分类名
     * @return 返回符合条件的分类信息列表
     */
    @ApiOperation(value="根据用户名模糊查询用户信息", notes="返回符合条件的用户信息列表")
    @ApiImplicitParam(name = "name", value = "用户名", required = true, dataType = "String", paramType = "path")
    @GetMapping("/search/{name}")
    public Result search(@PathVariable("name") String name) {
        QueryWrapper<WechatUser> wrapper = new QueryWrapper<>();
        wrapper.like("nickname", name);
        List<WechatUser> categoryList = wechatUserMapper.selectList(wrapper);
        if (categoryList.isEmpty()) {
            return Result.fail("暂无匹配结果");
        }
        return Result.succ(categoryList);
    }

}

